import 'package:aio/aio.dart';
import 'package:app/src/config/app_key.dart';

///数据存储缓存
class AppDataCache {
  /**
   * fixme 历史搜索记录
   */

  ///历史搜索记录
  static List<String>? _searchHistories = null;

  ///添加搜索记录
  static addHistoryWithSearch(String? search) {
    if (StringUtils.isNotEmpty(search)) {
      var list = getHistoryWithSearch();
      if (!list!.contains(search!)) {
        //list.add(search!);
        //历史记录，新加的放在前面。
        list.insert(0, search!);
        //存储到本地
        setHistoryWithSearch(list);
      }
    }
  }

  ///移除搜索记录
  static removeHistoryWithSearch(String? search) {
    if (StringUtils.isNotEmpty(search)) {
      var list = getHistoryWithSearch();
      list?.remove(search);
      //存储到本地
      setHistoryWithSearch(list);
    }
  }

  ///获取历史搜索记录
  static List<String>? getHistoryWithSearch() {
    if (StringUtils.isEmpty(_searchHistories)) {
      //优化获取用户输入的缓存域名
      _searchHistories =
          prefs()?.getStringList(AppKey.getKeyWithHistoryWithSearch());
    }
    if (_searchHistories == null) {
      _searchHistories = [];
    }
    return _searchHistories;
  }

  ///存储历史搜索记录
  static void setHistoryWithSearch(List<String>? searchHistories) {
    if (StringUtils.isEmpty(searchHistories)) {
      _searchHistories?.clear();
      prefs()?.remove(AppKey.getKeyWithHistoryWithSearch());
    } else {
      _searchHistories = searchHistories;
      prefs()?.setStringList(
          AppKey.getKeyWithHistoryWithSearch(), _searchHistories);
    }
  }

  /**
   * fixme 常用功能
   */

  ///常用功能
  static List<String>? _used = null;

  ///判断是否包含常用功能
  static bool containsUsedItems(String? YwxId) {
    if(_used==null){
      _used=getUsedItems();
    }
    if (_used != null && YwxId != null) {
      if (_used!.contains(YwxId)) {
        return true;
      } else {
        return false;
      }
    }
    return true;
  }

  ///添加搜索记录
  static addUsedItems(String? YwxId) {
    if (StringUtils.isNotEmpty(YwxId)) {
      var list = getUsedItems();
      if (!list!.contains(YwxId!)) {
        //list.add(search!);
        //历史记录，新加的放在前面。
        list.insert(0, YwxId!);
        //存储到本地
        setUsedItems(list);
      }
    }
  }

  ///移除搜索记录
  static removeUsedItems(String? YwxId) {
    if (StringUtils.isNotEmpty(YwxId)) {
      var list = getUsedItems();
      list?.remove(YwxId);
      //存储到本地
      setUsedItems(list);
    }
  }

  ///获取常用功能
  static List<String>? getUsedItems() {
    if (StringUtils.isEmpty(_used)) {
      //优化获取用户输入的缓存域名
      _used = prefs()?.getStringList(AppKey.getKeyWithUsed());
    }
    if (_used == null) {
      _used = [];
    }
    return _used;
  }

  ///存储常用功能
  static void setUsedItems(List<String>? used) {
    if (StringUtils.isEmpty(used)) {
      _used?.clear();
      prefs()?.remove(AppKey.getKeyWithUsed());
    } else {
      _used = used;
      prefs()?.setStringList(AppKey.getKeyWithUsed(), _used);
    }
  }

  ///存储待审核最新审核的时间
  static void setInsertDatetime(String? InsertDatetime) {
    if (StringUtils.isEmpty(InsertDatetime)) {
      prefs()?.remove(AppKey.getKeyWithInsertDatetime());
    } else {
      prefs()?.setString(AppKey.getKeyWithInsertDatetime(), InsertDatetime);
    }
  }

  ///获取待审核最新审核存储时间
  static String? getInsertDatetime() {
    return prefs()?.getString(AppKey.getKeyWithInsertDatetime());
  }

  ///存储最新工单时间
  static void setEquipmentInfoDatetime(String? EquipmentInfoDatetime) {
    if (StringUtils.isEmpty(EquipmentInfoDatetime)) {
      prefs()?.remove(AppKey.getKeyWithEquipmentInfoDatetime());
    } else {
      prefs()?.setString(AppKey.getKeyWithEquipmentInfoDatetime(), EquipmentInfoDatetime);
    }
  }

  ///获取最新工单时间
  static String? getEquipmentInfoDatetime() {
    return prefs()?.getString(AppKey.getKeyWithEquipmentInfoDatetime());
  }

}
